﻿using System;

using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;

using MosCraft.Interfaces;
using MosCraft.Utilities;
using MosCraft.Terrain.ChunkProviders;

namespace MosCraft.Terrain
{
    public class WorldProvider
    {
        public World world;
        private float []colorsSunriseSunset;

        public enum Worldtype { ALPHA, FLAT, PERLIN } 
        public Worldtype terrainType;

        public WorldProvider(World world)
        {
            this.world = world;
            this.colorsSunriseSunset = new float[4];
            this.terrainType = Worldtype.ALPHA;
        }

        public IChunkGenerator getChunkProvider()
        {
            switch (terrainType)
            {
                default:
                case Worldtype.FLAT:
                    return new ChunkProviderFlat(world, world.getSeed());

                case Worldtype.ALPHA:
                    return new ChunkProviderAlpha(world, world.getSeed());

                case Worldtype.PERLIN:
                    return new ChunkProviderPerlin(world, world.getSeed());

            }
        }

        public float calculateCelestialAngle(long time, float par3)
        {
            int i = (int)(time % 24000L);
            float angle = ((float)i + par3) / 24000F - 0.25F;

            if (angle < 0.0F)
                angle++;

            if (angle > 1.0F)
                angle--;

            float f1 = angle;
            angle = 1.0F - (float)((Math.Cos(angle * Math.PI) + 1.0D) / 2D);
            angle = f1 + (angle - f1) / 3F;
            return angle;
        }

        public Color4 getFogColor(float par1, float par2)
        {
            float f = QuickMath.Cos(par1 * MathHelper.Pi * 2.0F) * 2.0F + 0.5F;

            if (f < 0.0F)
                f = 0.0F;

            if (f > 1.0F)
                f = 1.0F;

            float R = 0.7529412F;
            float G = 0.8470588F;
            float B = 1.0F;

            R *= f * 0.94F + 0.06F;
            G *= f * 0.94F + 0.06F;
            B *= f * 0.91F + 0.09F;

            return new Color4(R, G, B, 255f);
        }

        public float[] calcSunriseSunsetColors(float par1, float par2)
        {
            float f = 0.4F;
            float f1 = QuickMath.Cos(par1 * (float)Math.PI * 2.0F) - 0.0F;
            float f2 = -0F;

            if (f1 >= f2 - f && f1 <= f2 + f)
            {
                float f3 = ((f1 - f2) / f) * 0.5F + 0.5F;
                float f4 = 1.0F - (1.0F - QuickMath.Sin(f3 * MathHelper.Pi)) * 0.99F;
                f4 *= f4;
                colorsSunriseSunset[0] = f3 * 0.3F + 0.7F;
                colorsSunriseSunset[1] = f3 * f3 * 0.7F + 0.2F;
                colorsSunriseSunset[2] = f3 * f3 * 0.0F + 0.2F;
                colorsSunriseSunset[3] = f4;
                return colorsSunriseSunset;
            }
            else
            {
                return null;
            }
        }

        public float getCloudHeight()
        {
            return 128F;
        }
    }
}
